package com.zkk.dao.impl;

import com.zkk.dao.UserDao;
import com.zkk.pojo.User;
import com.zkk.utils.DruidUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class UserDaoImpl implements UserDao {

     JdbcTemplate jdbcTemplate = new JdbcTemplate(DruidUtil.getDataSource());
    public User checkRegister(User user) {
        List<User> query = jdbcTemplate.query("select * from user where telephone = ? and password = ?", new BeanPropertyRowMapper<>(User.class), user.getTelephone(), user.getPassword());
        if(query!=null & query.size()==1){
            return query.get(0);
        }
        return null;
    }

    @Override
    public User existsTelephone(String telephone) {
        List<User> query = jdbcTemplate.query("select * from user where telephone = ?", new BeanPropertyRowMapper<>(User.class), telephone);
        if(query.size()==1 & query!=null){
            return query.get(0);
        }
        return null;
    }

    @Override
    public int addUser(User user) {
        int update = jdbcTemplate.update("insert into user(telephone,password) values (?,?)",user.getTelephone(), user.getPassword());
        return update;
    }

    @Override
    public User findTelPwd(User user) {
        List<User> query = jdbcTemplate.query("select * from user where telephone= ? and password = ?", new BeanPropertyRowMapper<>(User.class),user.getTelephone(),user.getPassword());
        if(query.size()==1 & query!=null){
            return query.get(0);
        }
        return null;
    }

    //开户添加数据

    @Override
    public int updateUser(User user) {
        int update = jdbcTemplate.update("update user set username=?,id_number=? where telephone =?", user.getUsername(), user.getId_number(), user.getTelephone());
        return update;
    }


}
